<?php
include_once "lib/config.php";
include_once "header.php";


if(isset($_POST['save'])&& $_POST['save']!="")
{
	//$arr_sanitize= $sen_obj->sanitize($_POST['Emailtemplate']);
	$arr_sanitize= $_POST['Emailtemplate'];
	if(isset($_POST['emailtemplate_id']) && $_POST['emailtemplate_id']>0)
	{
		$check_query=$general_obj->isexist_parameter($pdo_obj,"emailtemplates",array("type"=>$arr_sanitize["type"]),array("id"=>$_POST['emailtemplate_id']));
		if($check_query==0)
		{
		    $arr_sanitize['updated_by']=$_SESSION['u_id'];
            $arr_sanitize['update_date']=time();
            $arr_sanitize['added_by']=$_SESSION['u_id'];
            $arr_sanitize['add_date']=time();
			$pdo_obj->updatedata('emailtemplates',$arr_sanitize,array("id"=>$_POST['emailtemplate_id']));	
			if($pdo_obj->affectedrows>0)
			{
				$_SESSION['msg_success']="Email Template updated successfully.";
                redirect('emailtemplates.php');
			}
			else
			{
				$msg_error="There is no record updated.";
			}
		}
		else
		{
			$msg_error="There is already one document type exists of this name.";
		}
	}
	else
	{
		$check_query=$general_obj->isexist_parameter($pdo_obj,"emailtemplates",array("type"=>$arr_sanitize["type"]),array());
		
		if($check_query==0)
		{
			$arr_sanitize["status"]="Active";
            $arr_sanitize['updated_by']=$_SESSION['u_id'];
            $arr_sanitize['update_date']=time();
            
			$pdo_obj->insertData("emailtemplates",$arr_sanitize);
			if($pdo_obj->affectedrows>0)
			{
				$_SESSION['msg_success']="Email Template added successfully.";
				redirect('emailtemplates.php');
			}
			else
			{
				$msg_error="There is no record added.";
			}
			
		}
		else
		{
			$msg_error="There is already one document type exists of this name.";
		}
	}
	
}

if(isset($_POST['emailtemplate_id']) && $_POST['emailtemplate_id']>0)
{
	$condition=" id='".$_POST['emailtemplate_id']."'";
	$emailtemplateinfo=$general_obj->search_record($pdo_obj," * "," emailtemplates ",$condition,"id","asc",1);
}

?>
    
		<div class="container_12" id="main_div">
           <form action="" method="post" onsubmit="return validateaddeditemailtemplate();">
			<br>
			<div class="grid_12" >
		    	
		        <div class="module" >
		        		
		             <h2><span onclick="toggle_div('basic_body','toogleimg');" ><img src="images/desc.gif" height="5px" id="toogleimg">&nbsp;&nbsp;Email Template Details</span></h2>
		                
		             <div class="module-body" id="basic_body">
		                
		                  	<p>
		                        <label>Email Template Type :</label>
		                        <?php
                                if(isset($emailtemplateinfo[0]['type']))
                                {
                                    echo $emailtemplateinfo[0]['type'];
                                    ?>
                                    <input type="hidden" name="Emailtemplate[type]" id="type" value="<?php echo $emailtemplateinfo[0]['type']; ?>" />
                                    <?php
                                }
                                else
                                {
                                ?>
                                    <input type="text" style="width: 280px;" id="type" name="Emailtemplate[type]" value="<?php if(isset($emailtemplateinfo[0]['type'])){ echo $emailtemplateinfo[0]['type']; }else{ if(isset($arr_sanitize['type'])){ echo $arr_sanitize['type']; } } ?>" />
                                    <span style="color:red">*</span>
		                        <?php
                                }
                                ?>
		                    </p>
                            <p>
		                        <label>Keywords :</label>
		                        <span>
                                
                                    {EMPLOYEEFULLNAME},{EMPLOYEEFIRSTNAME},{EMPLOYEELASTNAME},{SUPERVISORNAME},{NEXTINCREMENTDATE},{PREVIOUSINCREMENTDATE},{LOGDATE},{RESIGNATIONDATE},{RELIEVINGDATE},{LEAVINGREASON},{BEFORE15DAYSOFRELIEVINGDATE},{BEFORE15DAYSOFBIRTHDATE},{BIRTHDATE},{BEFORE30DAYSOFNEXTINCREMENTDATE},{LASTWORKINGDATE}
                                    <input type="hidden" name="Emailtemplate[keywords]" value="{EMPLOYEEFULLNAME},{EMPLOYEEFIRSTNAME},{EMPLOYEELASTNAME},{SUPERVISORNAME},{NEXTINCREMENTDATE},{PREVIOUSINCREMENTDATE},{LOGDATE},{RESIGNATIONDATE},{RELIEVINGDATE},{LEAVINGREASON},{BEFORE15DAYSOFRELIEVINGDATE},{BEFORE15DAYSOFBIRTHDATE},{BIRTHDATE},{BEFORE30DAYSOFNEXTINCREMENTDATE},{LASTWORKINGDATE}" />
                                </span>
		                        
		                    </p>
                            <p>
		                        <label>Subject :</label>
		                        <textarea id="subject" name="Emailtemplate[subject]" cols="60" rows="3" ><?php if(isset($emailtemplateinfo[0]['subject']) ){ echo $emailtemplateinfo[0]['subject']; }else{ if(isset($arr_sanitize['subject'])){ echo  $arr_sanitize['subject']; } } ?></textarea>
		                        
		                    </p>
                            <p>
		                        <label>Body Message :</label>
		                        <textarea id="body" name="Emailtemplate[body]" cols="80" rows="10" ><?php if(isset($emailtemplateinfo[0]['body']) ){ echo $emailtemplateinfo[0]['body']; }else{ if(isset($arr_sanitize['body'])){ echo  $arr_sanitize['body']; } } ?></textarea>
		                        
		                    </p>
		                
		             </div> <!-- End .module-body -->
		
		        </div>  <!-- End .module -->
				
		    </div>
		    
		    <div class="grid_12">
		    <fieldset>
		    	&nbsp;
		    	<input type="hidden" id="emailtemplate_id" name="emailtemplate_id" value="<?php if(isset($_POST['emailtemplate_id'])){ echo $_POST['emailtemplate_id']; } ?>">
		        <input class="submit-green" type="submit" name="save" value="Submit" /> 
		        <input class="submit-gray" type="button" value="Back" onclick="window.location='emailtemplates.php'" />
		    </fieldset>
		    </div>
		    <div style="clear:both;"></div>
		 </form>    
        </div> <!-- End .container_12 -->
		
<?php include_once("footer.php"); ?>           
<?php
if(isset($msg_success) && $msg_success!="")
{
	$notymessage=$msg_success;
	$msgtype="success";
}
else if(isset($msg_error) && $msg_error!="")
{
	$msgtype="error";
	$notymessage=$msg_error;
}
else
{
	$msgtype="";
}
if($msgtype!="")
{
?>        
<script language="javascript">
	$(document).ready(function(){
		
		var options = $.parseJSON('{"text":"<?php echo $notymessage; ?>","layout":"top","type":"<?php echo $msgtype; ?>"}');
		noty(options);
		
	});
</script>  
<?php
}
?>
<script>
function validateaddeditemailtemplate()
{
    if(trim(document.getElementById('type').value)=="")
    {
        alertify.alert("Please select email template type.");
        return false;
    }
    else if(trim(document.getElementById('subject').value)=="")
    {
        alertify.alert("Please enter email subject.");
        return false;
    }
    else if(trim(document.getElementById('body').value)=="")
    {
        alertify.alert("Please enter email body message.");
        return false;
    }
}
</script>       